package org.duanyu.shopping_user_customer_api.controller;

import org.apache.dubbo.config.annotation.DubboReference;
import org.duanyu.shopping_common.pojo.ShoppingUser;
import org.duanyu.shopping_common.result.BaseResult;
import org.duanyu.shopping_common.service.MessageService;
import org.duanyu.shopping_common.service.ShoppingUserService;
import org.duanyu.shopping_common.util.RandomCodeUtil;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/user/shoppingUser")
public class ShoppingUserController {

    @DubboReference
    private MessageService messageService;
    @DubboReference
    private ShoppingUserService shoppingUserService;

    /**
     * 发送注册短信
     *
     * @param phone 用户的电话
     * @return 操作结果
     */
    @GetMapping("/sendMessage")
    public BaseResult sendMessage(String phone) {
        //1.生成随机的六位数验证码
        String code = RandomCodeUtil.buildCheckCode(6);
        BaseResult baseResult = messageService.sendMessage(phone, code);
        //发送成功，将验证码和手机号加入Redis中,发送失败则返回失败结果
        if (baseResult.getCode() == 200) {
            shoppingUserService.saveRegisterCheckCode(phone, code);
            return BaseResult.success();
        } else {
            return baseResult;
        }
    }

    /**
     * 验证用户验证码
     *
     * @param phone     手机号
     * @param checkCode 验证码
     * @return 200验证成功，605验证码错误异常
     */
    @GetMapping("/registerCheckCode")
    public BaseResult registerCheckCode(String phone, String checkCode) {
        shoppingUserService.registerCheckCode(phone, checkCode);
        return BaseResult.success();
    }

    /**
     * 用户注册
     *
     * @param shoppingUser 用户注册实体
     * @return 执行结果
     */
    @PostMapping("/register")
    public BaseResult register(@RequestBody ShoppingUser shoppingUser) {
        shoppingUserService.register(shoppingUser);
        return BaseResult.success();
    }


    /**
     * 用户名密码登录
     *loginPassword
     * @param shoppingUser 登录用户的实体类
     * @return 执行结果
     */
    @PostMapping("/loginPassword")
    public BaseResult loginPassword(@RequestBody ShoppingUser shoppingUser) {
        String sign = shoppingUserService.loginPassword(shoppingUser.getUsername(), shoppingUser.getPassword());
        return BaseResult.success(sign);
    }

    /**
     * 发送登录短信验证码
     *
     * @param phone 登录手机号
     * @return 执行结果
     */
    @GetMapping("/sendLoginCheckCode")
    public BaseResult sendLoginCheckCode(String phone) {
        //判断用户登录手机号是否状态正常
        shoppingUserService.checkPhone(phone);
        String code = RandomCodeUtil.buildCheckCode(4);
        BaseResult baseResult = messageService.sendMessage(phone, code);
        //消息发送成功，将账号和验证码结果存储到redis中,发送失败则返回失败结果
        if (baseResult.getCode() == 200) {
            shoppingUserService.saveLoginCheckCode(phone, code);
            return BaseResult.success();
        } else {
            return baseResult;
        }
    }

    /**
     * 手机号验证码登录
     *
     * @param phone     手机号
     * @param checkCode 验证码
     * @return 登录结果
     */
    @PostMapping("/loginCheckCode")
    public BaseResult loginCheckCode(String phone, String checkCode) {
        String sign = shoppingUserService.loginCheckCode(phone, checkCode);
        return BaseResult.success(sign);
    }

    /**
     * 获取登录的用户名
     *
     * @param authorization token令牌
     * @return 用户名
     */
    @GetMapping("/getName")
    public BaseResult<String> getName(@RequestHeader("Authorization") String authorization) {
        String token = authorization.replace("Bearer ", "");
        String username = shoppingUserService.getName(token);
        return BaseResult.success(username);
    }

}
